package com.newasia.management.shiro;

import org.apache.shiro.web.filter.authc.UserFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ShiroUserFilter extends UserFilter {

    /**
     * 该方法会在验证失败后调用，这里由于是前后端分离，后台不控制页面跳转
     * 因此重写改成传输JSON数据
     */
    @Override
    protected void saveRequestAndRedirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
        saveRequest(request);
//        PrintWriter out = response.getWriter();
//        ((HttpServletResponse)response).setStatus(401);
        ((HttpServletResponse)response).sendError(401,"未登录");
//        out.println(JSONObject.toJSONString("111"));
//        out.flush();
//        out.close();
    }
}
